package com.example.movie.controller;

import com.example.movie.entity.User;
import com.example.movie.feign.UserFeignClient;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.shared.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Set;
import java.util.logging.Logger;

@RestController
public class MovieController {

    private Logger logger = Logger.getLogger("MovieController");

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;


    @Value("${user.userServiceUrl}")
    private String userServiceUrl;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        System.out.print(this.userServiceUrl);
        logger.info("movie service request userid :" + id);
        return this.restTemplate.getForObject(this.userServiceUrl + id, User.class);
    }

    @GetMapping("/log-instance")
    public String logInstance(){
        ServiceInstance instance = this.loadBalancerClient.choose("USER-SERVICE");
        String result = instance.getServiceId() + ":" +instance.getHost() + ":" + instance.getPort() + ":" + instance.getUri();
        logger.info(result);
        return result;
    }


}
